package com.smarthome.web.property.controller.messagemanager;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.message.MessageRecordService;
import com.smarthome.core.message.util.MessageUtil;
import com.smarthome.domain.bean.ShSmsRecord;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.SmsTypeEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 短信记录
 * 
 * @author hgj
 * 
 */
@Controller
@RequestMapping("/messageRecordController")
public class MessageRecordController {

	@Resource
	private MessageRecordService messageRecordService;

	/**
	 * 获取短信记录表格
	 * 
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(PageHelper ph, ShSmsRecord record) {
		if(!UserHolder.isSuperAdmin()) record.setResidenceId(UserHolder.getCurrentResidenceId());
		return messageRecordService.dataGrid(ph, record);
	}
	
	/**
	 * 跳转到短信发送界面	  
	 * @return
	 */
	@RequestMapping("/tosend")
	public String sendPage(HttpServletRequest request) {
				
		return "/messageManager/messageSend";
	}

	/**
	 * 新增模版
	 * 
	 * @return
	 */
	@RequestMapping("/send")
	@ResponseBody
	public Result add(ShSmsRecord record) {
		try{
			MessageUtil.sendTimelyForContent(SmsTypeEnum.COMMON,UserHolder.getCurrentResidenceId(), record.getMobile(), record.getContent());
		} catch (Exception e){
			return Result.failure("短信发送出现异常");
		}
		return Result.success("短信已发送");
	}

}
